package bddaccess;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Iterator;

import modex.Terme;

public class TermeDAO extends DAO<Terme> {

	@Override
	public Terme create(Terme obj) {
		try {
			int id = 0;
			PreparedStatement prepare = this.connect
			                            	.prepareStatement(
			                            			"INSERT INTO Terme (idTerme, terme) VALUES(?, ?)",
			                            			Statement.RETURN_GENERATED_KEYS
			                            	);
			prepare.setInt(1, id);
			prepare.setString(2, obj.getValueTerme());
			
			prepare.executeUpdate();
			
			ResultSet rs = prepare.getGeneratedKeys();
			if (rs.next()){
			    id=rs.getInt(1);
			}
			obj = this.find(id);

	    } catch (SQLException e) {
	            e.printStackTrace();
	    }
	    return obj;
	}

	public void createMultiple(ArrayList<Terme> obj, int lot) {
		
		StringBuilder r = new StringBuilder();
		
		Iterator<Terme> it = obj.iterator();
		
		while(it.hasNext()){
			r.append("INSERT INTO Terme (idTerme, terme) VALUES ");
			Terme t = it.next();
			r.append("(").append(t.getIdTerme()).append(",'").append(t.getValueTerme()).append("')");
			for(int i=1; i<lot && it.hasNext(); i++){
				t = it.next();
				r.append(",");
				r.append("(").append(t.getIdTerme()).append(",'").append(t.getValueTerme()).append("')");
			}			
		}	
		
		try {
			this.connect.createStatement().executeUpdate(r.toString());
		} catch (SQLException e) {
			e.printStackTrace();
		}		
	}
	
	@Override
	public void delete(Terme obj) {
		try {			
            this.connect
               	.createStatement()
               	.executeUpdate(
               			"DELETE FROM Terme WHERE idTerme = " + obj.getIdTerme()
                );		
	    } catch (SQLException e) {
	            e.printStackTrace();
	    }
	}

	@Override
	public Terme find(int id) {
		Terme terme = new Terme();                
        
        try {
	        ResultSet result = this.connect
	                               .createStatement()
	                               .executeQuery(
	                                    "select * from Terme "+
	                                    "where idTerme = "+ id
	                                );

                if(result.first()){                	
                	terme.setIdTerme(id);
                	terme.setValueTerme(result.getString("terme"));
                }
                else{ return null; }
        } catch (SQLException e) {
                e.printStackTrace();
        }
        return terme;
	}
	
	public Terme find(String value) {
		Terme terme = new Terme();                
        
        try {
	        ResultSet result = this.connect
	                               .createStatement()
	                               .executeQuery(
	                                    "select * from Terme "+
	                                    "where terme = '"+ value+"'"
	                                );

                if(result.first()){                	
                	terme.setIdTerme(result.getInt("idTerme"));
                	terme.setValueTerme(result.getString("terme"));
                }
                else{ return null; }
        } catch (SQLException e) {
                e.printStackTrace();
        }
        return terme;
	}

	@Override
	public Terme update(Terme obj) {
		try {			
            this .connect	
                 .createStatement()
                 .executeUpdate(
                	"UPDATE Terme SET terme = '" + obj.getValueTerme() + "'"+
                	" WHERE idTerme = " + obj.getIdTerme()
                 );
		
            obj = this.find(obj.getIdTerme());
	    } catch (SQLException e) {
	            e.printStackTrace();
	    }	    
	    return obj;
	}
}
